package com.p2p.service.vipManagementService.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.p2p.common.DTO.BorrowerDTO;
import com.p2p.common.DTO.BorrowerSelectDTO;
import com.p2p.common.DTO.InvestorsDTO;
import com.p2p.common.base.RespResult;
import com.p2p.common.base.RespResultCode;
import com.p2p.entity.Borrower;
import com.p2p.mapper.BorrowerMapper;
import com.p2p.service.vipManagementService.BorrowerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;


@Service
@Slf4j
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements BorrowerService{
    @Autowired
    private BorrowerMapper borrowerMapper;

    /**
     * 添加一个借款用户
     * @param borrower 借款用户实体
     * @return RespResult
     */
    public RespResult addBorrower(Borrower borrower){
        log.info("管理员进行添加借款用户操作");

        if (borrower!=null){

            //根据当前时间戳生成唯一
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
            String number = sdf.format(System.currentTimeMillis());//202211041552160219

            //补充一个三位随机数
            int x=(int)(Math.random()*900)+100;
            String borrowerNum = number + x;
//            System.out.println("优化后-根据当前时间戳生成唯一："+borrowerNum);


            //自动生成借款用户编号
            borrower.setBorrowerNum(borrowerNum);
            //userSource用户来源
            borrower.setUserSource("管理员添加");

            borrowerMapper.insert(borrower);
            log.info("管理员进行添加借款用户：{}操作",borrower.getName()+"操作成功");
            return RespResult.success();
        }else {
            log.info("添加用户输入实体参数为空");
            return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(),RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);
        }
    }

    /**
     * 分页查询借款用户
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @return RespResult
     */
    @Override
    public RespResult findBorrowerList(Integer currentPage, Integer pageSize) {
        log.info("{}进行分页查询借贷用户操作",new Date());
        if (currentPage==null&&pageSize==null){
            currentPage=1;
            pageSize=10;
        }

        Page<BorrowerDTO> page=new Page<>(currentPage,pageSize);
        Page<BorrowerDTO> borrowerByPage = borrowerMapper.findBorrowerByPage(page);
        //返回分页的信息
        Map<String ,Object> borrowerMap=new HashMap<>();
        borrowerMap.put("count",borrowerByPage.getTotal());
        borrowerMap.put("borrowerData",borrowerByPage.getRecords());


        return RespResult.success(borrowerMap);
    }

    /**
     *根据借款用户id查询用户
     * @param id 借款用户id
     * @return RespResult
     */
    @Override
    public RespResult findBorrowerById(Long id){
        if (id!=null){
            Borrower borrower = borrowerMapper.selectById(id);
            return RespResult.success(borrower);
        }else {
            log.info("根据id查询用户输入实体参数为空");
            return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(),RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);
        }
    }

    /**
     * 修改借款用户信息
     * @param borrower 借款用户实体
     * @return RespResult
     */
    @Override
    public RespResult updateBorrower(Borrower borrower) {
        if (borrower!=null&&borrower.getId()!=null){
            borrowerMapper.updateById(borrower);
            return RespResult.success("更新成功");
        }else {
            log.info("修改用户输入实体参数为空");
            return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(),RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);
        }

    }

    /**
     * 更据借款用户id修改密码
     * @param id 用户id
     * @param loginPwd 登录密码
     * @param payPwd 付款密码
     * @return RespResult
     */
    @Override
    public RespResult updateBorrowerPwdById(Long id, String loginPwd, String payPwd) {
        if (id!=null&&loginPwd!=null&&payPwd!=null){
            borrowerMapper.updateBorrowerPwd(loginPwd,payPwd,id);
            return RespResult.success("修改密码成功");
        }else {
            log.info("修改借款用户密码输入实体参数为空");
            return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(),RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);
        }
    }

    /**
     * 根据条件查询借贷用户信息
     * @param borrowerSelectDTO 借贷用户信息
     * @return RespResult
     */
    @Override
    public RespResult findBorrowerByCondition(BorrowerSelectDTO borrowerSelectDTO) {
        if (borrowerSelectDTO!=null){
            if (borrowerSelectDTO.getCurrentPage()==null||borrowerSelectDTO.getPageSize()==null){
                borrowerSelectDTO.setCurrentPage(1);
                borrowerSelectDTO.setPageSize(10);
            }
            Page<BorrowerDTO> borrowerDTOPage=new Page<>(borrowerSelectDTO.getCurrentPage(),
                    borrowerSelectDTO.getPageSize());
            Page<BorrowerDTO> pageBorrower = borrowerMapper.findBorrowerByCondition(borrowerDTOPage,
                    borrowerSelectDTO.getName(),
                    borrowerSelectDTO.getPhone(),
                    borrowerSelectDTO.getLockStatus());

            Map<String, Object> objectMap=new HashMap<>();
            objectMap.put("count",pageBorrower.getTotal());
            objectMap.put("borrowerData",pageBorrower.getRecords());

            return RespResult.success(objectMap);

        }else {
            log.info("根据条件查询借贷用户参数为空");
            return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(),RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);
        }

    }

    /**
     * 根据借贷用户id删除借贷用户信息
     * @param id 借贷用户id
     * @return RespResult
     */
    @Transactional
    @Override
    public RespResult delBorrowerById(Long id) {
        if (id!=null){
            borrowerMapper.deleteById(id);
            borrowerMapper.deleteLoginDate(id);
            return RespResult.success("删除借款用户成功");
        }else {
            log.info("根据借贷用户id删除借贷用户参数为空");
            return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(),RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);
        }

    }
}
